package com.terrace.controller.system;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.terrace.core.common.model.ResultData;
import com.terrace.db.system.mapper.entity.User;
import com.terrace.core.utils.validation.ValidationHelper;
import com.terrace.web.annotation.Logs;
import com.terrace.web.constant.GlobalsConstant;

/**
 * 用户登录
 * @author jiangyg
 *
 */
@Controller
@Scope("prototype")
@RequestMapping("/login")
public class LoginController {
	
	/**
	 * 登录页面跳转
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView login(HttpServletRequest request) {
		Cookie[] cookies = request.getCookies();
		for (Cookie cookie : cookies) {
			if (StringUtils.endsWithIgnoreCase(cookie.getName(), "langType")) {
				request.getSession().setAttribute(GlobalsConstant.SYSTEM_LANG_TYPE, cookie.getValue());
			}
		}
		return new ModelAndView("login/login");
	}
	
	/**
	 * 用户登录验证
	 * @param request
	 * @return
	 */
	@ResponseBody
	@Logs(depict = "用户登录验证", type = GlobalsConstant.LogTypes.SYS_LOGIN)
	@RequestMapping(params = "check", method = RequestMethod.POST)
	public ResultData check(User loginUser, HttpServletRequest request) {
		ResultData result = new ResultData();
		ValidationHelper helper = ValidationHelper.valid(loginUser);
		if (helper.hasError()) {
			result.setResult(helper.getErrors());
			return result.failObjs("13105");
		}
		Subject user = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(loginUser.getUserAccount(), loginUser.getUserPwd());
		token.setRememberMe(true);
		try {
			user.login(token);
		} catch (AuthenticationException e) {
			token.clear();
			result.setStatus("13002");
		}
		return result;
	}
	
	/**
	 * 修改用户常用语类型
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(params = "alterLang", method = RequestMethod.POST)
	public ResultData alterLang(@RequestParam(value = "langType") String langType, HttpServletRequest request) {
		ResultData result = new ResultData();
		request.getSession().setAttribute(GlobalsConstant.SYSTEM_LANG_TYPE, langType);
		return result;
	}
	
	/**
	 * 用户退出登录
	 * @param request
	 * @return
	 */
	@RequestMapping(params = "logout")
	public ModelAndView logout(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.logout();
		return new ModelAndView("login/login");
	}

}
